import processing.sound.*;

PFont font1, font2, font3, font4;

SoundFile sndPickUp;
SoundFile sndDropDn;

SoundFile sndBark1;
SoundFile sndBark2;
SoundFile sndBark3;
SoundFile sndBark4;
SoundFile sndCenterBtn;
SoundFile sndNewBorn;
SoundFile sndError;

PImage cursorNormal, cursorDragging;
PImage logoL, logoR;

// ========================== //
// use a thread to laod sound files. then main window shown up faster
void preLoad() {
  sndPickUp = new SoundFile(this, "pick_up.mp3");
  sndDropDn = new SoundFile(this, "drop_down.mp3");

  sndBark1 = new SoundFile(this, "bark1.mp3");
  sndBark2 = new SoundFile(this, "bark2.mp3");
  sndBark3 = new SoundFile(this, "bark3.mp3");
  sndBark4 = new SoundFile(this, "bark4.mp3");

  sndCenterBtn = new SoundFile(this, "centerBtn.mp3");
  sndNewBorn = new SoundFile(this, "newBorn.mp3");
  sndError = new SoundFile(this, "error.mp3");
}

// ========================== //

void setup() {
  thread("preLoad");

  frameRate(20);
  size(1800, 1000);
  smooth();
  strokeWeight(3);
  rectMode(CENTER);
  colorMode(HSB, 360, 100, 100);
  noCursor(); // use image directly draw on screen as cursor

  logoL = loadImage("logoL.png");
  logoR = loadImage("logoR.png");

  font1 = loadFont("Rockwell-Condensed-128.vlw");
  font2 = loadFont("ScriptMTBold-128.vlw");
  font3 = loadFont("VinerHandITC-128.vlw");
  font4 = loadFont("BlackadderITC-Regular-128.vlw");

  cursorNormal = loadImage("cursor1.png");
  cursorDragging = loadImage("cursor2.png");  

  // make several demos at power up
  newDogAlien();
  newAntennaDogAlien();
  newTailDogAlien();
  newWingDogAlien();

  // random spread
  for (cdogAlien i : allDogAlien) {
    i.x = (int)random(100, 1700);
    i.y = (int)random(100, 200);
  }
  // No2 is specail case, put to fixed position
  allDogAlien[2].x = 907;
  allDogAlien[2].y = 490;
  
  println("Ver1.0");
}

// ========================== //
cdogAlien draggingDog = null;
boolean inDragging = false;

cdogAlien hoverDog = null;

void draw() {
  background(#E4DCF2); // clear screen with background color

  image(logoL, 10, 400);
  image(logoR, 1580, 400);

  // key press (lower case) 'd' 'a' 'w' to new a Alien Dog
  if (keyPressed) {
    if (cntTotal < 15) {
      switch (key) {
      case 'd':
        {
          newDogAlien();
          sndNewBorn.play();
          break;
        }
      case 'a':
        {
          newAntennaDogAlien();
          sndNewBorn.play();
          break;
        }
        // note: only one newTailDogAlien dog for the player
      case 'w':
        {
          newWingDogAlien();
          sndNewBorn.play();
          break;
        }
      }
    }else{
      sndError.play();
    }
    delay(200); // blocked key debouncing
  }

  // paint of alien dogs
  for (cdogAlien i : allDogAlien) {
    if (!inDragging) {
      if (i.mouseInRange(mouseX, mouseY)) {
        hoverDog = i; // mouse cursor hover on this dog
      }
    }

    if (draggingDog == i) {
      continue; // don't paint the dragging dog, it will be painted later. otherwise it's double image
    }
    i.paint();
  }

  // show mouse hover dog detail
  if (hoverDog != null) {
    hoverDog.showDetail();
    hoverDog = null;
  }

  // check if dog in dragging
  if (inDragging && draggingDog != null) {
    draggingDog.x = constrain(mouseX, dragRange, width - dragRange);
    draggingDog.y = constrain(mouseY, dragRange, height - dragRange);
    draggingDog.paint(); // paint the dragging dog here, then it's in top layer of other dogs
    image(cursorDragging, mouseX - 86, mouseY - 100);
  } else {
    image(cursorNormal, mouseX, mouseY);
  }

  // information on screen
  textFont(font1);
  textSize(60);

  fill(#85DBF0);
  text("Alien Dog  Colosseum. Ver1.0, Aug 2024", 28, 60);

  textSize(30);
  fill(#898FD3);
  text("press 'd' 'a' 'w' to new a Alien Dog.", 22, height - 30);
  textSize(40);
  text("Freda Li", width - 150, height - 20);

  textFont(font2);
  textSize(20);
  fill(#515CCE);
  text("DA\nADA\nTDA\nWDA\ntotal", width - 200, 30);
  text(nf(cntDA, 0, 0) + "\n" + nf(cntADA, 0, 0) + "\n" + nf(cntTDA, 0, 0) + "\n" + nf(cntWDA) + "\n" + nf(cntTotal, 0, 0), width - 80, 30);
}

// ========================== //

void mouseDragged() {
  if (!inDragging) {
    if (mouseButton == LEFT) {
      // println("drag " + mouseX + " " + mouseY);
      for (cdogAlien i : allDogAlien) {
        if (i.mouseInRange(mouseX, mouseY)) {
          sndPickUp.play();
          draggingDog = i;
          inDragging = true;
          break;
        }
      }
    }
  }
}

// ========================== //
// drag release and click release
void mouseReleased() {
  draggingDog = null;
  if (inDragging) {
    sndDropDn.play(); // only play sound for drag release
  }
  inDragging = false;
}

// ========================== //

void mouseClicked() {
  println("click " + mouseX + " " + mouseY); // easy to find out XY, debug purpose
  for (cdogAlien i : allDogAlien) {
    if (i.mouseInRange(mouseX, mouseY)) {
      if (mouseButton == LEFT) {
        i.bark();
      } else if (mouseButton == CENTER) {
        int blood = i.blood;
        if (blood < bloodMax) {
          sndCenterBtn.play();
          i.blood = min(blood + 10, bloodMax);
        }
      }
    }
  }
}
